// datetime
//

function Datetime (timeStr) {
  // 分解时间字符串
  var array = timeStr.split('T')
  var tmpDate = array[0]
  var tmpTime = array[1]
  var dateArray = tmpDate.split('-')
  var timeArray = tmpTime.split(':')
  this.year = dateArray[0]
  this.month = dateArray[1]
  this.day = dateArray[2]
  this.hour = timeArray[0]
  this.minute = timeArray[1]
  this.second = timeArray[2].substring(0, 2)

  this.getYear = function() {
    return this.year
  }

  this.getDay = function () {
    return this.day
  }

  this.getMonth = function () {
    return this.month
  }

  this.getHour = function () {
    return this.hour
  }

  this.getMinute = function () {
    return this.minute
  }

  this.getSecond = function () {
    return this.second
  }

  this.getDateStr = function () {
    var dateStr = this.year + '年' + this.month + '月' + this.day + '日'
    return dateStr
  }

  this.getDatetimeStr = function () {
    var datetimeStr = this.year + '-' + this.month + '-' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this.second
    return datetimeStr
  }

  this.getPastSeconds = function() {
    var now = new Date()
    var timeStr = this.year + '/' + this.month + '/' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this.second
    var time = new Date(timeStr)
    var delta = Math.floor((now.getTime() - time.getTime())/1000)
    return delta
  }

  this.getPastTime = function () {
    var now = new Date();
    var timeStr = this.month + ' ' + this.day + ',' + this.year  + ' ' + this.hour + ':' + this.minute + ':' + this.second
    var time = new Date(timeStr)
    var delta = (now.getTime() - time.getTime())/1000
    if (delta < 60) {
      return '刚刚'
    } else if (delta >= 60 && delta < 3600) {
      var minute = Math.floor(delta / 60)
      return minute + '分钟前'
    } else if (delta >= 3600 && delta < 86400) {
      var hour = Math.floor(delta / 3600)
      return hour + '小时前'
    } else if (delta >= 86400 && delta < 2592000) {
      var day = Math.floor(delta / 86400)
      return day + '天前'
    } else if (delta >= 2592000 && delta < 31104000) {
      var month = Math.floor(delta / 2592000)
      return month + '月前'
    } else if (delta > 31104000) {
      var year = Math.floor(delta / 31104000)
      return year + '年前'
    }
  }
}

export default Datetime
